﻿using Entity.ProjectEnt;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZFine.Application.Project;
using ZFine.Code;

namespace Logiv.WEB.Areas.Project.Controllers
{
    public class PickCarController : BaseController
    {
        private PathwayApp gudaoapp =new PathwayApp();
        private GudaoCarApp gudaocarapp=new GudaoCarApp();
        private CustomerAndThingsNameApp customerthings=new CustomerAndThingsNameApp();
        private CarriageApp serverApp = new CarriageApp();
        private CarriageBoxApp carriageBoxApp = new CarriageBoxApp();
        private CustomerAndThingsNameApp CTApp = new CustomerAndThingsNameApp();
        private CarTypeApp carTypeApp = new CarTypeApp();
        public override ActionResult Index()
        {
            var gudaolist = gudaoapp.GetList(f => f.State == true);
            ViewBag.gudaolist = gudaolist.ToJson();
            var pingminglist = customerthings.GetList(f => f.State == true && f.Type == 0);
            ViewBag.pingminglist = pingminglist.ToJson();
            var thingsName = customerthings.GetList(f => f.State == true && f.Type == 1);
            ViewBag.thingsName = thingsName.ToJson();

            var carType = carTypeApp.GetList();
            ViewBag.carType = carType.ToJson();
            return View();
        }

        /// <summary>
        /// 添加箱号页面
        /// </summary>
        /// <returns></returns>
        public ActionResult AddCarBox(string keyValue) {
            Pagination pagination2 = new Pagination();
            pagination2.sord = "F_CreatorTime desc";
            pagination2.sidx = "F_CreatorTime";
            pagination2.page = 1;
            pagination2.rows = 1000000000;
            try
            {
                List<CarriageBoxEntity> carriageBoxEntitieList = carriageBoxApp.GetListByCarriageCarCode(keyValue, pagination2);
                ViewBag.CarBoxList = carriageBoxEntitieList.ToJson();
                ViewBag.BagboxType = CTApp.GetList(f => f.Type == 2 && f.State == true)?.ToJson();
                return View();
            }
            catch (Exception ex)
            {

                throw;
            }
           
        }
        public JsonResult DelGudaoCar(List<object> thispar) {
            string msg = string.Empty;
            int code = 0;
            string data = Request["jsonData"];
            if (!string.IsNullOrEmpty(data))
            {
                List<CarAndgudao> objlist = ZFine.Code.Json.ToList<CarAndgudao>(data);
                if (objlist != null && objlist.Count > 0)
                {
                    objlist.ForEach(f => {
                        serverApp.DeleteFormByCarcode(f.chehao);
                    });
                    msg = "删除成功";
                }
                else {
                    code = -1;
                    msg = "传入数据为空";
                }
            }
            else {
                code = -1;
                msg = "传入数据为空";
            }
            return Json(new { msg = msg, code = code }, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 股道中添加车辆
        /// </summary>
        /// <param name="thispar"></param>
        /// <returns></returns>
        public JsonResult AddGudaoCar(List<object> thispar)
        {
            string msg = string.Empty;
            int code = 0;
            string data = Request["jsonData"];
            if (!string.IsNullOrEmpty(data))
            {
                List<CarAndgudao> objlist = ZFine.Code.Json.ToList<CarAndgudao>(data);
                if (objlist != null && objlist.Count > 0)
                {
                    var carcongfu = objlist.GroupBy(f => f.chehao).Where(f => f.Count() > 1).FirstOrDefault();
                    if (carcongfu != null)
                    {
                        code = -1;
                        msg = string.Format("{0}车号重复",carcongfu.FirstOrDefault().chehao);
                    }
                    else
                    {
                        code = gudaocarapp.SubmitAddCarToGudao(objlist, ref msg);
                    }


                }
                else
                {
                    code = -1;
                    msg = "转换对象为空";
                }
            }
            else
            {
                code = -1;
                msg = "传入数据为空";
            }
            return Json(new { msg = msg, code = code }, JsonRequestBehavior.AllowGet);
        }


        public JsonResult AddCarriageBoxList(List<object> thispar) {
            string msg = string.Empty;
            int code = 0;
            string data = Request["jsonData"];
            if (!string.IsNullOrEmpty(data))
            {
                List<CarriageBoxEntity> objlist = ZFine.Code.Json.ToList<CarriageBoxEntity>(data);
                if (objlist != null && objlist.Count > 0) {

                    var carcongfu = objlist.GroupBy(f => f.CarriageBoxID).Where(f => f.Count() > 1).FirstOrDefault();
                    if (carcongfu != null)
                    {
                        code = -1;
                        msg = string.Format("{0}箱号重复", carcongfu.FirstOrDefault().CarriageBoxID);
                    }
                    else
                    {
                        code = carriageBoxApp.SubmitAddCarBox(objlist, ref msg);
                    }
                }

            }
            else {
                code = -1;
                msg = "传入数据为空";
            }
            return Json(new { msg = msg, code = code }, JsonRequestBehavior.AllowGet);
        }
    }
   
}